package initAll;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Beta;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.aventstack.extentreports.ExtentTest;

import javaUnit.ThreadTime;
import seleniumUnit.linkOperate;

public class initHomePage{ 
	private static final Logger log=LogManager.getLogger(initHomePage.class.getName());
	private static String url = "https://www.baidu.com/";
	WebDriver driver;
	ExtentTest test;
	linkOperate Operate; 
	
	public initHomePage(ExtentTest test,WebDriver driver) {
		this.driver=driver;
		this.test=test;
		PageFactory.initElements(driver, this);
		Operate=new linkOperate(test,driver);
	}
	
//	@FindAll({
//    @FindBy(id = "A"),
//    @FindBy(id = "B")
//})
//public List aAndB	
//@FindAll相当于是取并集，如找到id=A和id=B的所有元素

//@Findbys({
//    @FindBy(className = "A"),
//    @FindBy(className = "B")
//})	
//driver.findElement(FindByA>).findElement(FindByB>)
//@FindBys 相当于是取交集，是先根据第一个注解获取到对应元素，然后根据第二个注解再帅选出对应的页面元素

//	@CacheLookup 意思是说找到元素之后将缓存元素，重复的使用这些元素，将使测试的速度大大加快。
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='新闻']")
	WebElement linkNews;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='hao123']")
	WebElement linkHao123;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='地图']")
	WebElement linkMap;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='直播']")
	WebElement linkLive;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='视频']")
	WebElement linkHaokan;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='贴吧']")
	WebElement linktieba;
	
	@FindBy(how=How.XPATH,xpath="//div[@id='s-top-left']/a[text()='学术']")
	WebElement linkXueshu;
	
	@FindBy(how=How.XPATH,xpath="//a[text()='更多' and @name='tj_briicon']")
	WebElement xuantingBtnMore;

	@FindBy(how=How.XPATH,xpath="//a[@name='tj_wangpan']")
	WebElement linkMorePan;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_zhidao']")
	WebElement linkMoreZhidao;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_baike']")
	WebElement linkMoreBaike;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_img']")
	WebElement linkMoreImage;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_baobaozhidao']")
	WebElement linkMoreBaobaozhidao;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_wenku']")
	WebElement linkMoreWenku;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_jingyan']")
	WebElement linkMorejingyan;
	
	@FindBy(how=How.XPATH,xpath="//a[@name='tj_mp3']")
	WebElement linkMoreMusic;
	
	@FindBy(how=How.XPATH,xpath="//a[text()='查看全部百度产品 >' and contains(@name,'tj_more')]")
	WebElement linkMoreBaiduALL;
	
	@FindBy(how=How.XPATH,xpath="//span[text()='设置' and @name='tj_settingicon']")
	WebElement xuantingSetting;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='搜索设置']")
	WebElement linkSettingSousuo;
	
	@FindBy(how=How.XPATH,xpath="//li[@data-tabid='general']")
	WebElement windowSettingSousuo;
	
	@FindBy(how=How.XPATH,xpath="//a[text()='恢复默认']")
	WebElement btnDefaultSetting;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='高级搜索']")
	WebElement linkSettingGaoji;
	
	@FindBy(how=How.XPATH,xpath="//li[@data-tabid='advanced']")
	WebElement windowSettingGaoji;
	
	@FindBy(how=How.XPATH,xpath="//span[@title='关闭']")
	WebElement windowSettingClose;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='关闭预测']")
	WebElement linkSettingYuceClose;
	
	@FindBy(how=How.XPATH,xpath="//a[@href='javascript:;' and text()='开启预测' ]")
	WebElement linkSettingYuceOpen;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='隐私设置']")
	WebElement linkSettingYinsi;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='关闭热榜']")
	WebElement linkSettingRebangClose;
	
	@FindBy(how=How.XPATH,xpath="//div/a[text()='开启热榜']")
	WebElement linkSettingRebangOpen;
	
	@FindBy(how=How.XPATH,xpath="//div/a[@id='s-top-loginbtn' and text()='登录']")
	WebElement btnLogin;
	
	@FindBy(how=How.XPATH,xpath="//p[@title='用户名登录']")
	WebElement btnLoginUserName;
	
	@FindBy(how=How.XPATH,xpath="//input[@id='TANGRAM__PSP_11__userName']")
	WebElement editUserName;
	
	@FindBy(how=How.XPATH,xpath="//input[@id='TANGRAM__PSP_11__password']")
	WebElement editPassword;
	
	@FindBy(how=How.XPATH,xpath="//input[@id='TANGRAM__PSP_11__submit']")
	WebElement btnYes;
	
	@FindBy(how=How.XPATH,xpath="//span[contains(@class,'user-name') and text()='Dai2014D']")
	WebElement textUserName;
	
	@FindBy(how=How.XPATH,xpath="//a[text()='退出登录']")
	WebElement exitLogin;
	
	@FindBy(how=How.XPATH,xpath="//span [text()='退出']")
	WebElement btnExitLogin;
	
	@FindBy(how=How.XPATH,xpath="//div/img [@id='s_lg_img']")
	WebElement linImage;
	
	@FindBy(how=How.ID,id="kw")
	WebElement serachEdit;		
	
	@FindBy(how=How.ID,id="su")
	WebElement serachBtn;	
	
	@FindBy(how=How.CSS,css="#result_logo > img.index-logo-src")
	WebElement homePage;
	
	public boolean linkNewsClick() {
		getUrl(url);
		return Operate.moveEleClickLink(linkNews,"百度新闻");
	}
	
	public boolean linkHao123Click() {
		getUrl(url);
		return Operate.moveEleClickLink(linkHao123,"hao123");
	}
	
	public boolean linkMap() {
		getUrl(url);
		return Operate.moveEleClickLink(linkMap,"百度地图");
	}
	
	public boolean linkLive() {
		getUrl(url);
		return Operate.moveEleClickLink(linkLive,"百度直播");
	}
	
	public boolean linkHaokan() {
		getUrl(url);
		return Operate.moveEleClickLink(linkHaokan,"好看视频");
	}
	
	public boolean linktieba() {
		getUrl(url);
		return Operate.moveEleClickLink(linktieba,"百度贴吧");
	}
	
	public boolean linkXueshu() {
		getUrl(url);
		return Operate.moveEleClickLink(linkXueshu,"百度学术");
	}
	
	
	public boolean moveClicklinkMorePan() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMorePan,"百度网盘");
	}
	
	public boolean moveClicklinkMoreZhidao() {
		getUrl(url);
		return Operate.moveEleClickLink(linkMoreImage,xuantingBtnMore,linkMoreZhidao,"百度知道");
	}
	
	public boolean moveClicklinkMoreBaike() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreBaike,"百度百科");
	}
	
	public boolean moveClicklinkMoreImage() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreImage,"百度图片");
	}
	
	public boolean moveClicklinkMoreBaobaozhidao() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreBaobaozhidao,"百度宝宝知道");
	}
	
	public boolean moveClicklinkMoreWenku() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreWenku,"百度文库");
	}
	
	public boolean moveClicklinkMorejingyan() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMorejingyan,"百度经验");
	}
	
	public boolean moveClickllinkMoreMusic() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreMusic,"千千音乐");
	}
	
	public boolean moveClicklinkMoreBaiduALL() {
		getUrl(url);
		return Operate.moveEleClickLink(xuantingBtnMore,linkMoreImage,linkMoreBaiduALL,"百度产品大全");
	}
	
	public void  moveClickSettingSousuo() {
		Operate.actionClick(driver,xuantingSetting,linkSettingSousuo);
		if(Operate.isElement(driver,"//li[@data-tabid='general']","xpath")) {
			if(windowSettingSousuo.getAttribute("class").equals("item cur")) {
				log.info("打开搜索设置窗口");
				btnDefaultSetting.click();
				ThreadTime.sleepTime(1);
				Operate.alertClick(driver,true);
				ThreadTime.sleepTime(1);
			}else {
				log.error("打开搜索设置窗口错误");
			}
		}
	}
	
	public void  moveClickSettingGaoji() {
		Operate.actionClick(driver,xuantingSetting,linkSettingGaoji);
		if(Operate.isElement(driver,"//li[@data-tabid='advanced']","xpath")) {
			if(windowSettingGaoji.getAttribute("class").equals("item cur")) {
				log.info("打开高级搜索设置窗口");
				windowSettingClose.click();
				ThreadTime.sleepTime(1);
			}else {
				log.error("打开高级搜索设置窗口错误");
			}
		}
	}
	
	public void  moveClickYuceCloseAndOpen() {
		System.out.println("driver1="+driver.toString());
		System.out.println("Operate1="+driver.toString());
		Operate.actionClick(driver,xuantingSetting);
		ThreadTime.sleepTime(1);
		if(linkSettingYuceClose.isDisplayed()) {
			linkSettingYuceClose.click();
			return;
		}
		if(linkSettingYuceOpen.isDisplayed()) {
			linkSettingYuceOpen.click();
			return;
		}
	}
	
	public void  moveClickRebangCloseAndOpen() {
		Operate.actionClick(driver,xuantingSetting);
		ThreadTime.sleepTime(1);
		if(linkSettingRebangClose.isDisplayed()) {
			linkSettingRebangClose.click();
			return;
		}
		if(linkSettingRebangOpen.isDisplayed()) {
			linkSettingRebangOpen.click();
			return;
		}
	}
	
	public boolean moveClicklinkSettingYinsi() {
		return Operate.moveEleClickLink(xuantingSetting,linkSettingYinsi,"登录百度帐号");
	}
	
	public void loginExit(String userName,String password) {
		btnLogin.click();
		Operate.getClickWebElement(driver,"//p[@title='用户名登录']","xpath",20);
		btnLoginUserName.click();
		Operate.getClickWebElement(driver,"//input[@id='TANGRAM__PSP_11__userName']","xpath",20);
		editUserName.clear();
		editUserName.sendKeys(userName);
		editPassword.sendKeys(password);
		btnYes.click();
		Operate.getClickWebElement(driver,"//span[contains(@class,'user-name') and text()='Dai2014D']","xpath",20);
		ThreadTime.sleepTime(3);
		Operate.actionClick(driver,textUserName);
		Operate.getClickWebElement(driver,"//a[text()='退出登录']","xpath",20);
		exitLogin.click();
		ThreadTime.sleepTime(1);
		btnExitLogin.click();	
	}
	
	
	public void serachEditIn(String inputStr) {
		 serachEdit.sendKeys(inputStr);
	}
	
	public void serachBtnClick() {
		serachBtn.click();
	}
	
	public void return_HomePage() {
		homePage.click();
	}
	
	public void clickMapBtn() {
		linkMap.click();
	}
	
	public void getUrl(String ieUrl) {
		String CurrentUrl=driver.getCurrentUrl();
		if(!driver.getCurrentUrl().equals(ieUrl)) {
			log.error("URL Error!"+"CurrentUrl="+CurrentUrl+"   targetURL="+ieUrl);
			driver.get(ieUrl);
		}
	}
}
